package top.zxybird.yun.rpc.api;

import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

public abstract class AbstractInvoker implements Invoker {

  @Getter
  @Setter
  private String serverIp;

  /**
   * 要连接的服务端的端口号.
   */
  @Getter
  @Setter
  private int serverPort;

  /**
   * 连接的超时时长.
   */
  @Getter
  @Setter
  private int timeout;

  public AbstractInvoker(URL url) {

    Preconditions.checkArgument(null != url, "Param url should not be null!");
    Preconditions.checkArgument(!StringUtils.isEmpty(url.getHost()), "Param url.host shoul not be empty!");
    Preconditions.checkArgument(null != url.getPort() && url.getPort() > 0, "Param url.port should be > 0!");
    // set timeout default value
    if (null == url.getTimeout() || url.getTimeout() <= 0) {
      url.setTimeout(2000);
    }

    this.serverIp = url.getHost();
    this.serverPort = url.getPort();
    this.timeout = url.getTimeout();
  }

}
